//
//  PublicArticleTableViewCell.m
//  AAIM
//
//  Created by 远的小布丁 on 2020/1/21.
//  Copyright © 2020 AAIM. All rights reserved.
//

#import "PublicArticleTableViewCell.h"

@implementation PublicArticleTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier withType:(NSString *)type{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor = KHEXCOLOR(@"#F4F4F4");
        [self showUI];
    }
    return self;
}

- (void)showUI{
    
    self.backView = [[UIView alloc]initWithFrame:CGRectMake(15, 0, kScreenWidth-30, 235.f)];
    self.backView.backgroundColor = [UIColor whiteColor];
    self.backView.layer.cornerRadius = 5;
    self.backView.layer.masksToBounds = YES;
    [self.contentView addSubview:self.backView];
    
    self.picImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.backView.width, 149)];
    self.picImg.image = [UIImage imageNamed:@"timg"];
     //设置切哪个直角
    //    UIRectCornerTopLeft     = 1 << 0,  左上角
    //    UIRectCornerTopRight    = 1 << 1,  右上角
    //    UIRectCornerBottomLeft  = 1 << 2,  左下角
    //    UIRectCornerBottomRight = 1 << 3,  右下角
    //    UIRectCornerAllCorners  = ~0UL     全部角
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.picImg.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(5,5)];
        //创建 layer
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = self.picImg.bounds;
        //赋值
        maskLayer.path = maskPath.CGPath;
        self.picImg.layer.mask = maskLayer;
    
    [self.backView addSubview:self.picImg];
    
    self.titleLbl = [[UILabel alloc]initWithFrame:CGRectMake(13, self.picImg.bottom+15, self.backView.width-30, 21)];
    self.titleLbl.textColor = KHEXCOLOR(@"#262B2F");
    self.titleLbl.font = kMainFont(15.f);
    self.titleLbl.text = @"年度好文，2020设计趋势";
    [self.backView addSubview:self.titleLbl];
    
    self.contentLbl = [[BaseLabel alloc]initWithFrame:CGRectMake(13, self.titleLbl.bottom+8, self.titleLbl.width, 27.f)];
    self.contentLbl.textColor = KHEXCOLOR(@"#AFAFAF");
      self.contentLbl.font = kMainFont(12.f);
    self.contentLbl.numberOfLines = 0;
    self.contentLbl.lineBreakMode = NSLineBreakByCharWrapping;
//    self.contentLbl.textInsets = UIEdgeInsetsMake(-60*SCALAE, 0, 60*SCALAE, 0);
    self.contentLbl.text = @"每天分享国内外湖两旺与投资消息速递，第一时间与您分享";
    
    self.contentLbl.height = [self calculationCellHeight:self.contentLbl.text];
      [self.backView addSubview:self.contentLbl];
    CGFloat labelHeight = [self calculationCellHeight:self.contentLbl.text];
    self.backView.height = labelHeight+208.f;
}

- (void)setModel:(ChildMenusModel *)model{
    _model = model;
    if (model.messageType == PublicMessagePicAndText) {

    self.contentLbl.text = model.describe;
    KWeakSelf;
    [self.picImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",userHeaderImage,model.pic]] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        weakSelf.picImg.image = image;
    }];
    self.titleLbl.text = model.title;
    self.contentLbl.height = [self calculationCellHeight:self.contentLbl.text];
         [self.backView addSubview:self.contentLbl];
       CGFloat labelHeight = [self calculationCellHeight:self.contentLbl.text];
       self.backView.height = labelHeight+208.f;
            
    }else{
        self.contentLbl.text = model.describe;
    }
}

- (CGFloat)calculationCellHeight:(NSString *)content{
    
    CGRect rect = [content boundingRectWithSize:CGSizeMake(self.backView.width-30, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: kMainFont(12)} context:nil];
    return rect.size.height;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
